import Vue from 'vue'
import {
  Toast,
  Dialog
} from 'vant'

import defaultImg from "@/assets/images/image_default.png"
Vue.use(Toast)
let preHandler = function (e) {
  e.preventDefault()
}
export default {
  data() {
    return {}
  },
  created () { 
  },
  mounted() {
    Toast.setDefaultOptions({
      duration: 1200
    }),
    Dialog.setDefaultOptions({
      // messageAlign: "left",
      cancelButtonColor: "#303133",
      confirmButtonColor: "#FF2525",
      closeOnPopstate: true
    });
    // app推送消息进入流量主消息中心
    this.$bridge.registerHandler("msgCenterByApp", (data, responseCallback) => {
      if(this.$route.name === "msgCenter"){
        window.location.reload();
      }else{
        // data为ObjC传递给JS的数据 
        this.$router.push({
          path:"/infMine/msgCenter",
          query:{
            uniqueId:data
          }
        });
      }
      // js返回数据给Objc
      responseCallback('success')
    });
  },
  methods: { 

    // 手机号 校验
    checkIsPhone(str) {
      var regmobile = /^0?1[3|4|5|7|8|9|6][0-9]\d{8}$/s
      if (regmobile.test(str)) {
        return true
      } else {
        return false
      }
    },
    // 身份证 校验
    IdentityCodeValid(code) {
      // 正则表达式：
      var idcardReg = /^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$|^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/

      if (idcardReg.test(code)) {
        // 合法
        return true
      } else {
        return false
      }
    },
    //非空判断
    isNull(str) {
      if (str === null || str === undefined || str === '' || str.length === 0 || str === 'undefined' || str === 'null') {
        return true;
      } else {
        return false;
      }
    },
    //获取url后面参数
    hashQuery() {
      var url = window.location.href //获取url中"?"符后的字串
      var theRequest = {}
      if (url.indexOf('?') !== -1) {
        var arr = url.split('?')
        var str
        if (arr[2] !== undefined) {
          str = arr[2]
        } else {
          str = arr[1]
        }
        var strs = str.split('&')
        if (strs.length === 0) {
          strs = [str]
        }
        for (var i = 0; i < strs.length; i++) {
          theRequest[strs[i].split('=')[0]] = unescape(strs[i].split('=')[1])
        }
      }
      return theRequest
    },
    // 当前距离顶部的滚动值
    getScrollTop() {
      var scrollTop = 0
      if (document.documentElement && document.documentElement.scrollTop) {
        scrollTop = document.documentElement.scrollTop
      } else if (document.body) {
        scrollTop = document.body.scrollTop
      }
      return scrollTop
    },
    // 屏幕的高度值
    getClientHeight() {
      var clientHeight = 0
      if (document.body.clientHeight && document.documentElement.clientHeight) {
        clientHeight = Math.min(
          document.body.clientHeight,
          document.documentElement.clientHeight
        )
      } else {
        clientHeight = Math.max(
          document.body.clientHeight,
          document.documentElement.clientHeight
        )
      }
      return clientHeight
    },
    // 滚动条的长度
    getScrollHeight() {
      return Math.max(
        document.body.scrollHeight,
        document.documentElement.scrollHeight
      )
    },
    //跳转界面
    goUrl(url, params) {
      if (params) {
        this.$router.push({
          name: url,
          query: params
        })
      } else {
        this.$router.push(url)
      } 
    },
    //懒加载图片 图片类型可以自己if加 type
    defaultImg(type) {
      let defaultImg = '';
      if (type == 1) {
        defaultImg = require('@/assets/images/image_default_z.png');
      } else if (type == 2) {
        defaultImg = require('@/assets/images/image_default.png');
      }else{
        defaultImg = require('@/assets/images/image_default_z.png');
      }
      return defaultImg;
    },
    loadImg(srcArr, callback) {
      if(!this.isNull(srcArr)){
        if (typeof (srcArr) == 'string') {
          let img = new Image();
          img.src = srcArr;
          img.onload = function () {
            callback(srcArr);
          };
        } else {
          let img = new Image();
          for (let i = 0; i < srcArr.length; i++) {
            img.src = srcArr[i];
            img.onload = function () {
              callback(srcArr);
            };
          }
        }
      }
     
    }, 
    //错误图片加载  图片类型可以自己if加 type
    errorImg(event) {
      let type = event.target.dataset.type;
      event.target.src = this.defaultImg(type);
    },
    //复制粘贴
    copy(dom) {
      let ClipboardJS = require('clipboard')
      let clipboard = new ClipboardJS(dom)
      //成功回调
      clipboard.on('success', function (e) {
        console.info('Text:', e.text)
        Toast.success({
          forbidClick: true,
          message: '复制成功'
        })
        e.clearSelection()
      })
      //失败回调
      clipboard.on('error', function (e) {
        // alert('失败1：' + e.text)
        console.error('Action:', e.action)
        console.error('Trigger:', e.trigger)
        Toast({
          forbidClick: true,
          message: '复制失败'
        })
      })
    },
    //校验输入的网址是否正确
    isUrl(url) {
      if (url.length > 8) {
        let urlHttps = url.slice(0, 5)
        let urlHttp = url.slice(0, 4)
        if (urlHttps == 'https') {
          let https = url.slice(0, 8)
          if (https == 'https://') {
            return true
          } else {
            return false
          }
        } else if (urlHttp == 'http') {
          let http = url.slice(0, 7)
          if (http == 'http://') {
            return true
          } else {
            return false
          }
        } else {
          return false
        }
      } else {
        return false
      }
    },
    //判断是否滚动
    isScroll(isScroll) {
      if (isScroll) {
        document.body.removeEventListener('touchmove', preHandler, {
          passive: false
        })
        document.getElementById('myFiter').style.display = 'none'
      } else {
        document.body.addEventListener('touchmove', preHandler, {
          passive: false
        })
        document.getElementById('myFiter').style.display = 'block'
      }
    },
    // 时间戳转换成日期格式
    getTime(value) {
      if (value == null) {
        return '';
      } else {
        let date = new Date(value);
        let y = date.getFullYear(); // 年
        let MM = date.getMonth() + 1; // 月
        MM = MM < 10 ? ('0' + MM) : MM;
        let d = date.getDate(); // 日
        d = d < 10 ? ('0' + d) : d;
        let h = date.getHours(); // 时
        h = h < 10 ? ('0' + h) : h;
        let m = date.getMinutes(); // 分
        m = m < 10 ? ('0' + m) : m;
        let s = date.getSeconds(); // 秒
        s = s < 10 ? ('0' + s) : s;
        return {
          y: y,
          MM: MM,
          d: d,
          h: h,
          m: m,
          s: s
        };
      }
    },
    // 判断是否为 IOS
    checkIsIos() {
      let isiOS = !!navigator.userAgent.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
      console.log("tag:ios", isiOS)
      return isiOS;
    },
    // 判断是否为 Android
    checkIsAndroid() {
      let isAndroid = navigator.userAgent.indexOf('Android') > -1 || navigator.userAgent.indexOf('Adr') > -1;
      return isAndroid;
    },
  }
}